package com.health.userbackstage.web.control;

import com.health.userbackstage.pojo.result.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
@RequestMapping("/per")
public class PermissionController {

    @GetMapping("/login")
    public Result test1(){
        //获取当前登录的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null){
            String username = (String) authentication.getPrincipal();

            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        }
        return Result.success();
    }

    @RequestMapping("/getLoginUsername")
    public Result getUserName(){
        String username = null;
        //获取当前登录的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null){
            username = (String) authentication.getPrincipal();
        }
        return Result.success(username);
    }

}
